/*
 * @Author: suchen.ma ma_suchen@126.com
 * @Date: 2023-10-12 23:03:09
 * @LastEditors: suchen.ma ma_suchen@126.com
 * @LastEditTime: 2023-10-15 23:57:48
 * @FilePath: \SoftWare\User\APP\sys_process.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

#include "sys_process.h"
#include "main.h"
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

extern osMessageQId queue_key_eventHandle;
extern TIM_HandleTypeDef htim2;
extern TIM_HandleTypeDef htim3;
extern osSemaphoreId semaphore_update_status;

void sys_event_process(void)
{
    Type_InCall_info *info = get_InCall_info();

    osEvent ret = osMessageGet(queue_key_eventHandle, 10);
    if (osEventMessage == ret.status)
    {
        unsigned int floor = ret.value.v;
        // 找到该按钮对应的楼层

        if (floor & 0xFF000000) // 来自电梯控制板的数据
        {
            for (int i = 0; i < 16; i++)
            {
                floor_map_s *floor_info = &(info->floor_key[i]);
                if (floor_info->enable)
                {
                    if (floor & (1 << (floor_info->dis_foor - 1)))
                    {
                        // 登记
                        floor_info->registered = true;
                        floor_info->pressed = false;
                        //                        floor_info->pressed_tick = xTaskGetTickCount();
                    }
                    else
                    {
                        if ((xTaskGetTickCount() - floor_info->pressed_tick) > protect_time)
                        {
                            floor_info->registered = false;
                            floor_info->pressed = false;
                        }
                    }
                    floor_info->upload_status = floor_info->registered || floor_info->pressed;
                }
            }
            osSemaphoreRelease(semaphore_update_status);
        }
        else
        {
            for (int i = 0; i < 16; i++)
            {
                if (floor & (1 << i))
                {
                    floor_map_s *floor_info = &(info->floor_key[i]);
                    if (floor_info->enable)
                    {

                        if (floor_info->registered == true)
                        {
                            // 取消登记 误指令删除，保留1s
                            info->delete_floor = floor_info->dis_foor;

                            info->delete_floor_start_tick = xTaskGetTickCount();

                            floor_info->pressed = false;
                        }
                        else
                        {
                            if (floor_info->pressed == false)
                            {
                                // 登记
                                floor_info->pressed = true;

                                floor_info->pressed_tick = xTaskGetTickCount();
                            }
                            else
                            {
                                // 取消登记 误指令删除，保留1s 为防止状态同步延时，此处保留删除楼层指令上报
                                info->delete_floor = floor_info->dis_foor;

                                info->delete_floor_start_tick = xTaskGetTickCount();

                                floor_info->pressed = false;
                            }
                        }
                        floor_info->upload_status = floor_info->pressed;
                    }
                }
            }
        }

        Type_InCall_info *info = get_InCall_info();
        floor_status_u floor_status;
        floor_status.value_short = 0;
        char _pressed_status = 0;
        for (int i = 0; i < 16; i++)
        {
            /* code */
            floor_map_s *floor_info = &(info->floor_key[i]);
            if (floor_info->pressed == true)
                _pressed_status = 1;
            if (floor_info->enable)
            {
                switch (floor_info->dis_foor)
                {
                case floor_1:
                    floor_status.value_bit.floor1 = floor_info->registered; //|| floor_info->pressed;
                    break;
                case floor_2:
                    floor_status.value_bit.floor2 = floor_info->registered; //|| floor_info->pressed;
                    break;
                case floor_3:
                    floor_status.value_bit.floor3 = floor_info->registered; //|| floor_info->pressed;
                    break;
                case floor_4:
                    floor_status.value_bit.floor4 = floor_info->registered; //|| floor_info->pressed;
                    break;
                case floor_5:
                    floor_status.value_bit.floor5 = floor_info->registered; // || floor_info->pressed;
                    break;
                case floor_6:
                    floor_status.value_bit.floor6 = floor_info->registered; //|| floor_info->pressed;
                    break;
                default:
                    break;
                }
                info->key_checked_val = floor_status.value_short;
            }
        }
        info->in_pressed_status = _pressed_status;
    }
    if ((xTaskGetTickCount() - info->delete_floor_start_tick) > 1500)
    {
        info->delete_floor = 0;
    }
}

void led_process(void)
{
    static bool led_status = 0;
    bool back_light_color = 0;
    Type_InCall_info *info = get_InCall_info();
    unsigned short hc595_val = 0;
    for (int i = 0; i < 16; i++)
    {
        /* code */
        floor_map_s *floor_info = &(info->floor_key[i]);
        if (floor_info->enable)
        {
            if (floor_info->registered == true)
            {
                hc595_val |= 1 << i;
                back_light_color = 1;
            }
            else if (floor_info->pressed == true)
            {
                back_light_color = 1;
                if (led_status == true)
                {
                    hc595_val |= 1 << i;
                }
            }
        }
    }
    led_status = !led_status;
    if (hc595_val != info->hc595_dis_val)
    {
        mc74hc595_1_write(hc595_val);
        mc74hc595_2_write(hc595_val);
        info->hc595_dis_val = hc595_val;
    }
    if (1 == back_light_color)
    {
        info->work_mode = 1;
        HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3);
        HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4);
        __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, 0);
        __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_4, led_light);

        HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
        HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
        __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, 0);
        __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, led_light);
    }
    else
    {
        info->work_mode = 0;
        HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3);
        HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4);
        __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, led_light);
        __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_4, 0);

        HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
        HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
        __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, led_light);
        __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, 0);
    }
}
